/**
 * Created by Cheney on 2017/8/11.
 */


function array( arr ) {
  this.arr = arr
}

array.prototype.get = function () {
    return this.arr;
}

array.prototype.filterEq = function (key, value) {
  var n = [];
  for( var i in this.arr ){
    if( this.arr[i][key] == value ){
      n.push(this.arr[i]);
    }
  }
  return n;
}

array.prototype.orderBy = function (key) {
  return this.arr.sort(function (a, b) {
    if( a[key] > b[key] ){
      return 1;
    }
    if( b[key] < a[key] ){
      return -1;
    }
    return 0;
  })
}

array.prototype.groupBy = function (key) {
  var arr = this.arr.sort(function (a, b) {
    if( a[key] > b[key] ){
      return 1;
    }
    if( b[key] < a[key] ){
      return -1;
    }
    return 0;
  })
  var lastKey;
  var map = {};
  for( var i in arr ){
    if( lastKey != arr[i][key] ){
      lastKey = arr[i][key];
      map[lastKey] = [arr[i]];
    }else{
      map[lastKey].push(arr[i]);
    }
  }
  return map;
}


module.exports = function(arr) {
  return new array(arr);
}
